package org.sjs.dreamfly.data

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import org.sjs.dreamfly.common.util.LogUtil
import org.sjs.dreamfly.model.MovieInfo

@Dao
interface MovieInfoDao {
    @Query("SELECT * FROM movie_info where channel=:channel")
    fun getMovieList(channel: String?): List<MovieInfo>
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(movieInfo: MovieInfo)

    @Query("SELECT * FROM movie_info WHERE id = :movieId")
    suspend fun query(movieId: String): List<MovieInfo>

    suspend fun updateOrInsert(movieInfo: MovieInfo) {
        query(movieId = movieInfo.id).firstOrNull().let {
            if (it != null) {
                LogUtil.d("id:${it.id} ${it.name} is existed")
                movieInfo.id = it.id
            }
            insert(movieInfo, it?.channel)
        }
    }

    suspend fun insert(movieInfo: MovieInfo, channel: String?) {
        movieInfo.channel = channel
        this.insert(movieInfo)
    }


//    @Query("DELETE FROM movie_info")
//    fun deleteMovieInfo()
//    @Query("ALTER TABLE movie_info ADD COLUMN newField TEXT")
//    fun addNewField()
}